package com.smh.a19exerciseDemo6;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.Callable;

public class MyThread implements Callable<ArrayList<Integer>> {


    ArrayList<Integer> sList;

    public MyThread(ArrayList<Integer> sList) {
        this.sList = sList;
    }

    @Override
    public ArrayList<Integer> call() throws Exception {
        ArrayList<Integer> list = new ArrayList<>();
        while (true) {
            synchronized (MyThread.class) {
                if (sList.size() == 0) {
                    break;
                } else {
                    Thread.sleep(1000);
                    Random random = new Random();
                    int index = random.nextInt(sList.size());
                    System.out.println(Thread.currentThread().getName());
                    if (Thread.currentThread().getName().contains("1")) {
                        list.add(sList.get(index));
                    } else {
                        list.add(sList.get(index));
                    }

                    sList.remove(index);
                }
            }
        }
        return list;
    }
}
